package com.example.educationai.service.impl;

import com.example.educationai.entity.SimpleUser;
import com.example.educationai.repository.SimpleUserRepository;
import com.example.educationai.service.SimpleUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.List;

@Slf4j
@Service
public class SimpleUserServiceImpl implements SimpleUserService {

    @Autowired
    private SimpleUserRepository userRepository;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public SimpleUser register(SimpleUser user) {
        log.debug("开始注册用户: {}", user.getUsername());
        if (existsByUsername(user.getUsername())) {
            throw new RuntimeException("用户名已存在");
        }
        if (existsByEmail(user.getEmail())) {
            throw new RuntimeException("邮箱已被注册");
        }
        // 加密密码
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        SimpleUser savedUser = userRepository.save(user);
        log.debug("用户注册成功: {}", savedUser.getUsername());
        return savedUser;
    }

    @Override
    public SimpleUser login(String username, String password) {
        log.debug("尝试登录用户: {}", username);
        SimpleUser user = findByUsername(username);
        if (user == null) {
            log.debug("用户不存在: {}", username);
            throw new RuntimeException("用户不存在");
        }
        if (!passwordEncoder.matches(password, user.getPassword())) {
            log.debug("密码错误: {}", username);
            throw new RuntimeException("密码错误");
        }
        log.debug("用户登录成功: {}", username);
        return user;
    }

    @Override
    public SimpleUser getUser(Long id) {
        return userRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("用户不存在"));
    }

    @Override
    public List<SimpleUser> getAllUsers() {
        return userRepository.findAll();
    }

    @Override
    public SimpleUser updateUser(SimpleUser user) {
        if (!userRepository.existsById(user.getId())) {
            throw new RuntimeException("用户不存在");
        }
        return userRepository.save(user);
    }

    @Override
    public void deleteUser(Long id) {
        userRepository.deleteById(id);
    }

    @Override
    public boolean existsByUsername(String username) {
        return userRepository.existsByUsername(username);
    }

    @Override
    public SimpleUser findByUsername(String username) {
        log.debug("查找用户: {}", username);
        return userRepository.findByUsername(username)
                .orElse(null);
    }

    @Override
    public SimpleUser findByEmail(String email) {
        return userRepository.findByEmail(email)
                .orElse(null);
    }

    @Override
    public boolean existsByEmail(String email) {
        return userRepository.existsByEmail(email);
    }
} 